/*
 * ====================================================================
 *
 * Copyright (c) 2002 Caravel Technologies. All rights reserved.
 *
 * For information about the company, visit <http://www.caraveltech.com>.
 *
 * ====================================================================
 */
package ex1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.util.MessageResources;

/**
 * Implementation of <strong>Action</strong> that accepts input for the user
 * profile.
 *
 * @author Wellie W. Chao
 * @version $Revision: 1.1 $ $Date: 2002/05/26 16:25:26 $
 */
public final class ProfileAction extends Action {

    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an
     * <code>ActionForward</code> instance describing where and how control
     * should be forwarded, or
     * <code>null</code> if the response has already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if business logic throws an exception
     */
    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm baseForm,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        ProfileForm form = (ProfileForm) baseForm;
        HttpSession session = request.getSession();
        
        
        
        // Remove the obsolete form bean
        if (mapping.getAttribute() != null) {
            if ("request".equals(mapping.getScope())) {
                request.removeAttribute(mapping.getAttribute());
            } else {
                session.removeAttribute(mapping.getAttribute());
            }
        }

        // Place form values in request scope for bean:write tags
        request.setAttribute("firstName", form.getFirstName());
        request.setAttribute("lastName", form.getLastName());
        request.setAttribute("favoriteColor", form.getFavoriteColor());
        request.setAttribute("birthdate", form.getBirthdate());
        request.setAttribute("address", form.getAddress());
        request.setAttribute("phone", form.getPhone());

        // Forward control to the specified success URI
        return (mapping.findForward("success"));
    }
}
